package com.oocl.o2o.merchant.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.oocl.o2o.merchant.dao.FoodTypeDao;
import com.oocl.o2o.merchant.entity.FoodType;

@Repository
public class FoodTypeDaoImpl implements FoodTypeDao {
	@PersistenceContext
	private EntityManager em;

	@Override
	public FoodType add(FoodType t) {
		try {
			em.persist(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public FoodType update(FoodType t) {
		try {
			t = em.merge(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public FoodType delete(String id) {
		try {
			FoodType foodTypeInDb = em.find(FoodType.class, id);
			 em.remove(foodTypeInDb);
			 return foodTypeInDb;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public FoodType findById(String id) {
		try {
			return  em.find(FoodType.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<FoodType> findAll() {
		
		try {
			String jpql = "select f from FoodType f";
			TypedQuery<FoodType> query = em.createQuery(jpql, FoodType.class);
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
